<template>
  <div>
    <!-- <ul class="list">
      <li class="item" v-for="(item,index) in cities" :key="index" @click="handleLetterClick" @touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd">
        {{ index }}
      </li>
    </ul> -->
    <ul class="list">
      <!-- v-for="(item,index) in cities"  -->
      <!-- {{ index }} -->
      <li class="item" v-for="item in letters" :key="item" :ref="item" @click="handleLetterClick" @touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd">
        {{ item }}
      </li>
    </ul>
  </div>
</template>



<script>
// @import '~styles/varibles.styl'
import axios from "axios";
export default {
  props: {
    cities: Object
    // cities: []
  },
  data() {
    return {};
  },
  computed: {
    letters() {
      const letters = [];
      for (let i in this.cities) {
        letters.push(i);
      }
      return letters;
    }
  },
  // created() {
  //   axios.get("static/city.json").then(res => {
  //     console.log(res);
  //     this.cities = res.data.data.hotCities;
  //     //     this.cities = res.data.data.cities;
  //   });
  // },
  methods: {
    handleLetterClick(e) {
      console.log(e.target.innerText);
      this.$emit("change", e.target.innerText); //发送
      // 本地存储
      // localStorage.setItem("change", e.target.innerText);
    },
    handleTouchStart() {
      this.touchStatus = true;
    },
    handleTouchMove(e) {
      if (this.touchStatus) {
        //A字符距离顶部的距离
        const startY = this.$refs["A"][0].offsetTop;
        console.log(startY);
        //所触控区距离最顶部距离e.touches[0].clientY   79为量出来量出来header高43+search高为36
        const touchY = e.touches[0].clientY - 79;
        console.log(touchY);
        // index触控位置的下标   20量为字符的高度
        const index = Math.floor((touchY - startY) / 20);
        console.log(index);
        if (index >= 0 && index < this.letters.length) {
          this.$emit("change", this.letters[index]);
        }
      }
    },
    handleTouchEnd() {
      this.touchStatus = false;
    }
  }
};
</script>


<style lang="stylus" scoped>
.list {
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: absolute;
  box-sizing: border-box;
  top: 1.58rem;
  right: 0.2rem;
  bottom: 0;
  width: 0.6rem;
  text-align: center;
  margin-top: 2rem;
  // background-color: red;
  font-size: 0.4rem;
  color: #00b2c8;
  position: fixed;

  // background :red
  .item {
    text-align: center;
    color: $bgColor;
    line-height: 1rem;
  }
}
</style>
